﻿using System;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Torch.FrameWork.Langeuages;
using Torch.FrameWork.Utility;
using Torch.FrameWork.Common;

namespace Torch.WL.Web.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                               
                btn_login.Attributes.Add("onclick", "return checklogin()");

                //取得当前系统的语言版本
                string[] strluanage = Request.ServerVariables.Get("HTTP_ACCEPT_LANGUAGE").ToLower().Split(',');

                //语言类型样式下拉选择框。
                NameValueCollection nvc = (NameValueCollection)ConfigurationManager.GetSection("WebLanguage");
                ArrayList Languagelist = new ArrayList();//语言列表

                for (int i = 0; i < nvc.Count; i++)
                {
                    Language.Items.Add(new ListItem(nvc[nvc.GetKey(i)], nvc.GetKey(i).ToLower()));
                    Languagelist.Add(nvc.GetKey(i).ToLower());
                }

                if (strluanage != null)
                {
                    //检测浏览器语言版本是否在语言列表中
                    bool aa = false;
                    for (int i = 0; i < Languagelist.Count; i++)
                    {
                        if (strluanage[0] == Languagelist[i].ToString())
                        {
                            aa = true;
                        }
                    }

                    if (aa)
                    {
                        HttpCookie WebLanguage = new HttpCookie("WebLanguage", strluanage[0].ToLower());
                        Session["userLanguage"] = strluanage[0].ToLower();
                        //默认语言选择
                        Language.SelectedValue = strluanage[0].ToLower();
                    }
                    else
                    {
                        string s = ConfigurationManager.AppSettings["InitLanguages"].ToLower();
                        //如果检测浏览器语言版在系统没有语言，则转成英文
                        HttpCookie WebLanguage = new HttpCookie("WebLanguage", s);
                        Session["userLanguage"] = s;
                        //默认语言选择
                        Language.SelectedValue = s;
                    }
                }

                UserName.Focus();

                #region 语言加入
                btn_login.Text = ResourceManager.GetString("Login_Btn_login");
                #endregion
            }
        }

        protected void btn_login_Click(object sender, EventArgs e)
        {
            Torch.WL.Business.UserLogic bll = new Torch.WL.Business.UserLogic();
            Torch.WL.Entity.UserEntity model = new Torch.WL.Entity.UserEntity();

            if (UserName.Text.Trim() == "" || Password.Text.Trim() == "")
            {
                PageHelper.ShowMessagebox(this, ResourceManager.GetString("Login_Msg_V_3"));
            }
            else
            {

                if (String.Compare(Request.Cookies["VerifyCode"].Value, Request.Form["txt_verify"].Trim().ToUpper(), true) != 0)
                {
                    PageHelper.ShowMessagebox(this, "验证码错误");
                    return;
                }

                if (bll.CheckLogin(UserName.Text.Trim(), CryptMethod.MD5(Password.Text.Trim(), 32)))
                {
                    LogEvent.LogInfo("登陆成功,UserName=[{0}]",UserName.Text.Trim());
                    model = bll.GetUserModel(UserName.Text.Trim());
                    if (model.RoleID.Count != 0 || model.IsLimit == true)
                    {
                        if (model.Status != 0)
                        {
                            bll.UpdateLoginTime(model.UserID);//更新登录时间
                            Session["userLanguage"] = Language.SelectedValue;

                            SessionHelper.CreateUserSession(
                                new UserSession(model.UserID, model.UserName, 
                                    model.RoleID, model.UserGroup, 
                                    model.IsLimit, model.Status)
                                    );


                            Response.Redirect("~/Admin/Default.aspx");
                        }
                        else
                        {                     
                            PageHelper.ShowMessagebox(this, ResourceManager.GetString("Login_Msg_2"));
                        }
                    }
                    else
                    {                 
                        PageHelper.ShowMessagebox(this, ResourceManager.GetString("Login_Msg_3"));
                    }
                }
                else
                {
                    LogEvent.LogInfo("登陆失败,UserName=[{0}],IP=[{1}]", UserName.Text.Trim() ,Utils.GetRealIP());
                    PageHelper.ShowMessagebox(this, ResourceManager.GetString("Login_Msg_2"));
                }
            }
        }
    }
}
